package com.che300.kotlin.extand

import android.os.Bundle
import android.os.Parcelable
import android.support.v4.app.Fragment
import java.io.Serializable

/**
 * @author hsh
 * @date 2017/9/14 17:07.
 * @doc
 */
fun Fragment.getString(key: String, defaultValue: String): String = this.arguments?.getString(key) ?: defaultValue

fun Fragment.getInt(key: String, defaultValue: Int): Int = this.arguments?.getInt(key, defaultValue) ?: defaultValue

fun Fragment.getBoolean(key: String, defaultValue: Boolean): Boolean = this.arguments?.getBoolean(key, defaultValue) ?: defaultValue

fun Fragment.getFloat(key: String, defaultValue: Float): Float = this.arguments?.getFloat(key, defaultValue) ?: defaultValue

fun Fragment.getDouble(key: String, defaultValue: Double): Double = this.arguments?.getDouble(key, defaultValue) ?: defaultValue

inline fun <reified T : Parcelable> Fragment.getParcelable(key: String): T? = this.arguments?.getParcelable(key)

inline fun <reified T : Serializable> Fragment.getSerializable(key: String): T? = this.arguments?.getSerializable(key) as T

fun Fragment.putString(key: String, value: String): Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putString(key, value)
    return this
}

fun Fragment.putInt(key: String, value: Int): Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putInt(key, value)
    return this
}

fun Fragment.putBoolean(key: String, value: Boolean): Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putBoolean(key, value)
    return this
}

fun Fragment.putFloat(key: String, value: Float): Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putFloat(key, value)
    return this
}

fun Fragment.putDouble(key: String, value: Double): Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putDouble(key, value)
    return this
}

fun Fragment.putParcelable(key: String, value: Parcelable): Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putParcelable(key, value)
    return this
}

fun Fragment.putSerializable(key: String, value: Serializable): Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putSerializable(key, value)
    return this
}


fun android.app.Fragment.getString(key: String, defaultValue: String): String = this.arguments?.getString(key) ?: defaultValue

fun android.app.Fragment.getInt(key: String, defaultValue: Int): Int = this.arguments?.getInt(key, defaultValue) ?: defaultValue

fun android.app.Fragment.getBoolean(key: String, defaultValue: Boolean): Boolean = this.arguments?.getBoolean(key, defaultValue) ?: defaultValue

fun android.app.Fragment.getFloat(key: String, defaultValue: Float): Float = this.arguments?.getFloat(key, defaultValue) ?: defaultValue

fun android.app.Fragment.getDouble(key: String, defaultValue: Double): Double = this.arguments?.getDouble(key, defaultValue) ?: defaultValue

inline fun <reified T : Parcelable> android.app.Fragment.getParcelable(key: String): T? = this.arguments?.getParcelable(key)

inline fun <reified T : Serializable> android.app.Fragment.getSerializable(key: String): T? = this.arguments?.getSerializable(key) as T

fun android.app.Fragment.putString(key: String, value: String): android.app.Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putString(key, value)
    return this
}

fun android.app.Fragment.putInt(key: String, value: Int): android.app.Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putInt(key, value)
    return this
}

fun android.app.Fragment.putBoolean(key: String, value: Boolean): android.app.Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putBoolean(key, value)
    return this
}

fun android.app.Fragment.putFloat(key: String, value: Float): android.app.Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putFloat(key, value)
    return this
}

fun android.app.Fragment.putDouble(key: String, value: Double): android.app.Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putDouble(key, value)
    return this
}

fun android.app.Fragment.putParcelable(key: String, value: Parcelable): android.app.Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putParcelable(key, value)
    return this
}

fun android.app.Fragment.putSerializable(key: String, value: Serializable): android.app.Fragment {
    if (this.arguments == null) this.arguments = Bundle()
    this.arguments.putSerializable(key, value)
    return this
}
